define('repair', function(require, exports, module) {

	var view = function(id) {
		var self = this;
		$.get(contextPath + '/repair/view/' + id).done(function(html) {
			addBreadcrumbMenu('查看');
			pageContents.html(html);
			init('view', id);
		});
	};
	var add = function() {
		var self = this;
		$.get(contextPath + '/repair/new').done(function(html) {
			addBreadcrumbMenu('新增');
			pageContents.html(html);
			init('add');
		});
	};
	var modify = function(id) {
		var self = this;
		$.get(contextPath + '/repair/update/' + id).done(function(html) {
			addBreadcrumbMenu('修改');
			pageContents.html(html);
			init('modify', id);
		});
	};
	var init = function(type, id) {
		pageContents.find('#returnBtn').on('click', function() {
			var children = breadcrumb.children();
			children[children.length - 2].click();
		});
		if(type != 'view'){
			pageContents.find('.form_datetime').datetimepicker({
				language : 'zh-CN',
				format : "yyyy-mm-dd hh:ii:ss",
				autoclose : true,
				todayBtn : true,
				pickerPosition : 'bottom-left'
			});
		}
		var form = pageContents.find('#form');
		var saveBtn = pageContents.find('#saveBtn');
		if (type == 'modify') {
			pageContents.find('.panel-title').text('修改报修信息');
			pageContents.find('#repairDateStr').attr('disabled', 'disabled').parent().find('.input-group-addon').off();
		} else if (type == 'view') {
			pageContents.find('.panel-title').text('查看报修信息');
			form.find('input, select').attr('disabled', 'disabled');
			saveBtn.hide();
			return;
		} else {
			pageContents.find('.panel-title').text('新增报修');
		}
		if (type == 'modify') {
			var status = pageContents.find('#status');
			if (status.data('value')) {
				status.val(status.data('value'));
			}
			var repairDateStr = pageContents.find('#repairDateStr');
			var maintenanceTimeStartStr = pageContents.find('#maintenanceTimeStartStr');
			var maintenanceTimeEndStr = pageContents.find('#maintenanceTimeEndStr');
            var expectedRepairTimeStr = pageContents.find('#expectedRepairTimeStr');
            repairDateStr.val() && repairDateStr.parent().datetimepicker('setDate', new Date(repairDateStr.val()));
			maintenanceTimeStartStr.val() && maintenanceTimeStartStr.parent().datetimepicker('setDate', new Date(maintenanceTimeStartStr.val()));
			maintenanceTimeEndStr.val() && maintenanceTimeEndStr.parent().datetimepicker('setDate', new Date(maintenanceTimeEndStr.val()));
            expectedRepairTimeStr.val() && expectedRepairTimeStr.parent().datetimepicker('setDate', new Date(expectedRepairTimeStr.val()));
        } else if(type == 'add'){
			pageContents.find('#repairDateStr').parent().datetimepicker('setDate', new Date()).closest('.form-group').hide();
			pageContents.find('#maintenanceTimeStartStr').closest('.form-group').remove();
			pageContents.find('#maintenanceTimeEndStr').closest('.form-group').remove();
			pageContents.find('#status').closest('.form-group').remove();
			pageContents.find('#maintenanceUser').closest('.form-group').remove();
			pageContents.find('#maintenancePhone').closest('.form-group').remove();
			pageContents.find('#contact').closest('.form-group').remove();
			pageContents.find('#contactPhone').closest('.form-group').remove();
		}
		require('util').initValidate(form);
		saveBtn.on('click', function() {
			var $btn = $(this);
			var form = pageContents.find('#form');
			if ($btn.hasClass('disabled')) {
				form.message({
					type : 'warning',
					content : '请不要重复提交!'
				});
				return;
			}
			$btn.addClass('disabled').attr('disabled', true);
			var util = require('util');
			if (!util.validate(form)) {
				$btn.removeAttr('disabled').removeClass('disabled');
				return;
			}
			var data = util.getFormData(form);
			$.post(contextPath + '/repair/save', data).done(function(data) {
				if (data.statusCode == 20011011) {
					$('body').message({
						type : 'success',
						content : '保存成功'
					});
					pageContents.find('#returnBtn').click();
				} else {
					pageContents.find('#form').message({
						type : 'error',
						content : data.msg
					});
					$btn.removeAttr('disabled').removeClass('disabled');
				}
			});
		});
	};
	var remove = function(ids, dataGrid) {
		var data = {
			delIds : ids.join('@')
		}
		$.post(contextPath + '/repair/delete', data).done(function(data) {
			if (data.statusCode == 20011011) {
				dataGrid.message({
					type : 'success',
					content : data.msg
				});
				dataGrid.grid('refresh');
			} else {
				dataGrid.message({
					type : 'error',
					content : data.msg
				});
			}
		})
	}
	module.exports = {
		view : view,
		add : add,
		modify : modify,
		remove : remove
	};
});
